package controllers

import (
	"encoding/json"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type CollectfeesController struct {
	BaseController
}

func (c *CollectfeesController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/collectfees/index"
}

func (c *CollectfeesController) Index() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	c.display("", "collectfees/index_footerjs.html")
}

//获取待结帐患者
func (c *CollectfeesController) GetWaitAccountUser() {
	kwd := c.GetString("kwd")
	datas := models.GetWaitAccountUser(c.curUser.DepartmentId, kwd)
	c.jsonDataTable(0, datas)
}


//保存结账信息
func (c *CollectfeesController) SaveAccountList() {
	c.checkAuthor("collectfees/index") //直接给列表权限

	model := new(models.SaCollectfeesDoctororder)
	model.CreatedBy = c.curUser.RealName
	model.DepartmentId = c.curUser.DepartmentId
	model.SumPayable, _ = c.GetFloat("sum_payable")
	model.CashMoney, _ = c.GetFloat("cash_money")
	model.CardMoney, _ = c.GetFloat("card_money")
	model.TotalMoney, _ = c.GetFloat("total_money")
	model.AlipayMoney, _ = c.GetFloat("alipay_money")
	model.WechatMoney, _ = c.GetFloat("wechat_money")
	archives_id, _ := c.GetInt64("archives_id")
	model.Archives = &models.Archives{Id: archives_id}
	model.DetailIds = c.GetString("doctor_ids")
	model.Status = 0 //正常状态
	ser := services.CollectfeesService{}
	if err := ser.SaCollectfeesDoctororderSave(model); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "结算成功", "")
}

//结帐记录
func (c *CollectfeesController) Logs() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	if c.Ctx.Input.IsPost() {
		//反序化获取json格式的requestbody里的值
		var params models.SaCollectfeesDoctororderQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		//获取数据列表和总数
		params.DepartmentId = c.curUser.DepartmentId
		data, total := models.SaCollectfeesDoctororderPageList(&params)
		c.jsonDataTable(total, data)
	}
	c.Data["activeSidebarUrl"] = "/collectfees/logs"
	c.display("", "collectfees/logs_footerjs.html")
}
//结帐明细
func (c *CollectfeesController) LogsDetail() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	if c.Ctx.Input.IsPost() {
		id,_ := c.GetInt64("id")
		//pay_status := c.GetString("pay_status")
		if id == 0 {
			c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		}
		if data,err := models.GetSaCollectfeesDoctororderById(id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}
	c.display("", "collectfees/logsdetail_footerjs.html", "shared/layout_dialog.html")
}

//门诊退费
func (c *CollectfeesController) LogsOut() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	if c.Ctx.Input.IsPost() {
		model := new(models.SaCollectfeesDoctororder)
		model.Id,_ = c.GetInt64("logs_id")
		model.CreatedBy = c.curUser.RealName
		model.DepartmentId = c.curUser.DepartmentId
		model.SumPayable, _ = c.GetFloat("sum_payable")
		model.CashMoney, _ = c.GetFloat("cash_money")
		model.CardMoney, _ = c.GetFloat("card_money")
		model.TotalMoney, _ = c.GetFloat("total_money")
		model.AlipayMoney, _ = c.GetFloat("alipay_money")
		model.WechatMoney, _ = c.GetFloat("wechat_money")
		archives_id, _ := c.GetInt64("archives_id")
		model.Archives = &models.Archives{Id: archives_id}
		model.DetailIds = c.GetString("doctor_ids")
		model.Status = 0 //正常状态
		ser := services.CollectfeesService{}
		if err := ser.SaCollectfeesDoctororderOut(model); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "退费成功", "")
	}
	c.display("", "collectfees/logsout_footerjs.html", "shared/layout_dialog.html")
}

//药房发药
func (c *CollectfeesController) Dispensing() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	if c.Ctx.Input.IsPost() {
		archives_id, _ := c.GetInt64("archives_id")
		detailIds := c.GetString("doctor_ids")
		ser := services.CollectfeesService{}
		if err := ser.SendDispensing(c.curUser, archives_id, detailIds); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "发药完成", "")

	}
	c.Data["activeSidebarUrl"] = "/collectfees/dispensing"
	c.display("", "collectfees/dispensing_footerjs.html")
}

//获取待发药患者
func (c *CollectfeesController) GetWaitDispensingUser() {
	kwd := c.GetString("kwd")
	datas := models.GetWaitDispensing(c.curUser.DepartmentId, kwd)
	c.jsonDataTable(0, datas)
}

//发药记录

func (c *CollectfeesController) DispensingLogs() {
	c.checkAuthor("collectfees/index") //直接给列表权限
	if c.Ctx.Input.IsPost() {
		//反序化获取json格式的requestbody里的值
		var params models.SaCollectfeesDispensingQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		//获取数据列表和总数
		params.DepartmentId = c.curUser.DepartmentId
		data, total := models.SaCollectfeesDispensingPageList(&params)
		c.jsonDataTable(total, data)
	}
	c.Data["activeSidebarUrl"] = "/collectfees/dispensing_logs"
	c.display("", "collectfees/dispensinglogs_footerjs.html")
}